/*
 * RemoteIO.h
 *
 * Created: 29.10.2013 16:11:13
 *  Author: Elektronik
 */ 
#pragma once


#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <string.h>
#include "util.h"
#include "timer0.h"
#include "system_config.h"
//=================================
#define INVERT_INPUTS 0
#define INVERT_OUTPUTS 0

//=================================
#if defined REMOTE_IO_MASTER || defined REMOTE_IO_SLAVE
	#define REMOTE_IO_DEFINED
#endif



#ifdef REMOTE_IO_DEFINED

//=================================

typedef struct RemoteIOOutputs_tag
{
	uint8_t out_0	: 1;
	uint8_t out_1	: 1;
	uint8_t out_2	: 1;
	uint8_t out_3	: 1;
	uint8_t out_4	: 1;
	uint8_t out_5	: 1;
	uint8_t out_6	: 1;
	uint8_t out_7	: 1;
	uint8_t out_8	: 1;
	uint8_t out_9	: 1;
	uint8_t out_pwm0;
	uint8_t out_pwm1;
	int16_t out_temp0;
	int16_t out_temp1;
	int16_t out_temp2;
	
} RemoteIOOutputs, *PRemoteIOOutputs;

//=========================================================================

typedef struct RemoteIOInputs_tag
{
	uint8_t in_0	: 1;
	uint8_t in_1	: 1;
	uint8_t in_2	: 1;
	uint8_t in_3	: 1;
	uint8_t in_4	: 1;
	uint8_t in_5	: 1;
	uint8_t in_6	: 1;
	uint8_t in_7	: 1;
	uint8_t in_8	: 1;
	uint8_t in_9	: 1;
	int16_t in_temp0;
	int16_t in_temp1;
	int16_t in_temp2;		
} RemoteIOInputs, *PRemoteIOInputs;

//=================================

#define SEGMENT_SIZE 1
#define CHECKUM_SIZE 2

#if defined REMOTE_IO_MASTER
	#define SEND_BUFFER_SIZE (SEGMENT_SIZE + sizeof(RemoteIOOutputs) + CHECKUM_SIZE)
	#define REC_BUFFER_SIZE (SEGMENT_SIZE + sizeof(RemoteIOInputs) + CHECKUM_SIZE)
#elif defined REMOTE_IO_SLAVE
	#define SEND_BUFFER_SIZE (SEGMENT_SIZE + sizeof(RemoteIOInputs) + CHECKUM_SIZE)
	#define REC_BUFFER_SIZE (SEGMENT_SIZE + sizeof(RemoteIOOutputs) + CHECKUM_SIZE)
#endif

#define REC_MSG_TIMEOUT 5
#define COM_FAIL_TIMEOUT 50;
#define RESEND_TIME 100
//===========================================================================

extern RemoteIOOutputs outputs;
extern RemoteIOInputs inputs;

//===========================================================================
#ifdef __cplusplus
	extern "C" {
#endif
//--------------------------------
void RemoteIO_Begin(uint8_t brd);
uint8_t IsRemoteIOCommOk();
void RemoteIO_Loop();
//--------------------------------
#ifdef __cplusplus
	};
#endif
//===========================================================================

#endif